package wms

import (
	"context"
	"encoding/json"
	"ez/apps/wms/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type AttributeValue struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64         `json:"id" bson:"id,omitempty"`
	K           *AttributeKey `json:"k" bson:"k"`                   //键
	KId         int64         `json:"kId" bson:"kId"`               //键
	ItemBase    *ItemBase     `json:"itemBase" bson:"itemBase"`     //物品
	ItemBaseId  int64         `json:"itemBaseId" bson:"itemBaseId"` //物品
	Name        string        `json:"name" bson:"name"`             //名称
	Sort        int64         `json:"sort" bson:"sort"`             //排序
	Code        string        `json:"code" bson:"code"`             //编码
	Note        string        `json:"note" bson:"note"`             //说明
	IsOn        bool          `json:"isOn" bson:"isOn"`             //是否使用
	CreateAt    time.Time     `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time     `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time    `json:"deleteAt" bson:"deleteAt"`
}

func (this *AttributeValue) DocName() string { return "AttributeValue" }
func (this *AttributeValue) GetId() int64    { return this.Id }
func (this *AttributeValue) SetId(id int64)  { this.Id = id }
func (this *AttributeValue) Create() error {
	return this.GetFactory().Create(this)
}
func (this *AttributeValue) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *AttributeValue) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *AttributeValue) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *AttributeValue) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *AttributeValue) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *AttributeValue) ToString() string {
	return string(this.ToBytes())
}
func (this *AttributeValue) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *AttributeValue) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *AttributeValue) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *AttributeValue) LoadK() {
	if this.KId == 0 {
		return
	}
	this.K, _ = NewAttributeKeyCrud().FindId(this.KId)
}
func (this *AttributeValue) LoadItemBase() {
	if this.ItemBaseId == 0 {
		return
	}
	this.ItemBase, _ = NewItemBaseCrud().FindId(this.ItemBaseId)
}
func (this *AttributeValue) ClearRelationsBeforeSave() mgo.Doc {
	this.K = nil
	this.ItemBase = nil
	return this
}
func neverUsed_AttributeValue() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type AttributeValueAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *AttributeValue
	Session *ez.Session
}

func NewAttributeValueAccessControl(model *AttributeValue, action string, session *ez.Session) *AttributeValueAccessControl {
	ctrl := &AttributeValueAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.AttributeValueAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
